#include "Player.h"

	Player::Player(float px, float py, int cw, int ch, SDL_Surface *image, int r, int c): ASprite(px, py, cw, ch, image, r, c)
	{
		canshoot = true;
		jumping = false;
		canjump = false;
		facing = Right;
		wallside = None;
		hang = false;
		dy = 0;
		dx = 0;
		jumpcount = 0;
		frametime = 0;
	}

	void Player::EventHandling(SDL_Event event)
	{
		//Handle user input

		Uint8 *keystate = SDL_GetKeyState(NULL);

		if(keystate[SDLK_LEFT])
		{
			facing = Left;
			MovingLeft = true;
		}
		else if(!keystate[SDLK_LEFT])
			MovingLeft = false;
		
		if(keystate[SDLK_RIGHT])
		{
			facing = Right;
			MovingRight = true;
		}
		else if(!keystate[SDLK_RIGHT])
			MovingRight = false;

		if(keystate[SDLK_UP])
		{
			facing = Up;
		}

		if(event.type == SDL_KEYDOWN)
		{
			switch(event.key.keysym.sym)
			{	
			case SDLK_a:
				if(canjump == true)
				{
					jumping = true;
					canjump = false;
					dy = -380;
				}
				break;
			}
		}

	}

	void Player::Update(Uint32 dticks)
	{
		Animate();
		frametime += dticks/1000.f;

		if(frametime >= 1)
		{
			frametime = 0;
			canshoot = true;
			//NextImage();
		}

		if(MovingLeft == true)
		{
			x += -250 * (dticks/1000.f);
		}

		if(MovingRight == true)
		{
			x += 250 * (dticks/1000.f);
		}

		y += dy * (dticks/1000.f);

		if(dy < 400)
			dy = dy + (1000*(dticks/1000.f));

		CollBox.x = x;
		CollBox.y = y;
	}

	void Player::Animate()
	{
		//Change animation based on state
		switch(facing)
		{
		case Left:
			if(MovingLeft)
			{
				if(jumping)
				{

				}
				else if(!jumping)
				{

				}
			}
			else if(!MovingLeft)
			{
				if(jumping)
				{

				}
				else if(!jumping)
				{
					if(hang)
					{
						//No hanging yet
					}
					else if(!hang)
					{
						setCurrentreel(2);
						setCurrentframe(0);
						setOscillation(false);
						animate(true);
					}
				}

			}
			break;
		case Right:
			if(MovingRight)
			{
				if(jumping)
				{

				}
				else if(!jumping)
				{

				}
			}
			else if(!MovingRight)
			{
				if(jumping)
				{

				}
				else if(!jumping)
				{
					if(hang)
					{
						//No hanging yet
					}
					else if(!hang)
					{
						setCurrentreel(1);
						setCurrentframe(0);
						setOscillation(false);
						animate(true);
					}
				}

			}
			break;
		case Up:
			setCurrentreel(7);
			setCurrentframe(0);
			setOscillation(false);
			animate(false);
			break;
		case Down:
			setCurrentreel(7);
			setCurrentframe(0);
			setOscillation(false);
			animate(false);
			break;
		case Upright:
			break;
		case Downright:
			break;
		case Upleft:
			break;
		case Downleft:
			break;
		case None:
			setCurrentreel(0);
			setCurrentframe(0);
			setOscillation(false);
			animate(true);
			break;
		}
	}
